{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### pry-docの読み込み\n",
    "#require \"/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\"\n",
    "require \"/Users/ftakao2007/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 数値型クラス\n",
    "\n",
    "* Numeric\n",
    "    * Integer (整数)\n",
    "        * Bignum (非常に大きな整数)\n",
    "        * Fixnum (通常の整数)\n",
    "    * Complex (複素数)\n",
    "    * Rational (有理数)\n",
    "    * Float (浮動小数点数)\n",
    "        * 計算の過程で誤差が発生する可能性がある\n",
    "        * シフト演算やビット演算は実装されていない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numeric\n",
      "Numeric\n",
      "Numeric\n",
      "Numeric\n",
      "Integer\n",
      "Numeric\n",
      "Integer\n",
      "Numeric\n",
      "[Numeric, Comparable, Object, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Float"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts Rational.superclass\n",
    "puts Complex.superclass\n",
    "puts Float.superclass\n",
    "puts Integer.superclass\n",
    "puts Fixnum.superclass\n",
    "puts Fixnum.superclass.superclass\n",
    "puts Bignum.superclass\n",
    "puts Bignum.superclass.superclass\n",
    "puts Numeric.ancestors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# メソッドとそのクラスの調べ方\n",
    "\n",
    "1. classメソッドでインスタンスのクラスを調べる\n",
    "    * <font color=\"red\">メソッドを実行した後のオブジェクトのクラスでは無い事に注意</font>\n",
    "1. show-docでドキュメントを表示する\n",
    "    * 事前にpry-docをインストールしておく\n",
    "1. public_methodなどでそのクラスのメソッドであることを実際に確認する\n",
    "    * ここまではやらなくても良いと思う"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceilメソッドを実行しているインスタンスのクラスを確認\n",
      "Float\n",
      "メソッド実行後のオブジェクトのクラスは変化している可能性がある(ここではFloat -> Fixnum)\n",
      "こちらは変化後のクラスなので今回調査する対象ではない\n",
      "Fixnum\n"
     ]
    }
   ],
   "source": [
    "puts \"ceilメソッドを実行しているインスタンスのクラスを確認\"\n",
    "puts 0.9.class\n",
    "puts \"メソッド実行後のオブジェクトのクラスは変化している可能性がある(ここではFloat -> Fixnum)\"\n",
    "puts \"こちらは変化後のクラスなので今回調査する対象ではない\"\n",
    "puts 0.9.ceil.class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Integer\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m ceil()\n",
      "\u001b[1mNumber of lines:\u001b[0m 3\n",
      "\n",
      "As \u001b[32mint\u001b[0m is already an Integer, all these methods simply return the receiver.\n",
      "\n",
      "Synonyms are #to_int, #floor, #ceil, #truncate.\n"
     ]
    }
   ],
   "source": [
    "### show-docでドキュメントを表示\n",
    "show-doc Fixnum#ceil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceilがそのクラス(Float)のメソッド(public method)であることを確認\n",
      "#<Method: Float#ceil>\n"
     ]
    }
   ],
   "source": [
    "puts \"ceilがそのクラス(Float)のメソッド(public method)であることを確認\"\n",
    "puts 0.9.public_method(:ceil)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Numericクラス\n",
    "\n",
    "## 小数点以下の切り捨て、切り上げ\n",
    "\n",
    "* ceil\n",
    "    * 自身と同じか自身より大きな整数を返す\n",
    "* floor\n",
    "    * 自身と同じか自身より小さな整数を返す\n",
    "* round\n",
    "    * 最も近い整数を返す (四捨五入)\n",
    "* truncate\n",
    "    * それ自身と0の間にある整数で最も近いものを返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceil\n",
      "1\n",
      "1\n",
      "2\n",
      "floor\n",
      "0\n",
      "1\n",
      "1\n",
      "round\n",
      "1\n",
      "2\n",
      "2\n",
      "truncate\n",
      "-1\n",
      "0\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "puts \"ceil\"\n",
    "puts 0.9.ceil\n",
    "puts 1.0.ceil\n",
    "puts 1.1.ceil\n",
    "\n",
    "puts \"floor\"\n",
    "puts 0.9.floor\n",
    "puts 1.0.floor\n",
    "puts 1.1.floor\n",
    "\n",
    "puts \"round\"\n",
    "puts 1.4.round\n",
    "puts 1.5.round\n",
    "puts 1.6.round\n",
    "\n",
    "puts \"truncate\"\n",
    "puts -1.1.truncate\n",
    "puts -0.7.truncate\n",
    "puts 0.3.truncate\n",
    "puts 1.7.truncate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Float\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m truncate()\n",
      "\u001b[1mNumber of lines:\u001b[0m 3\n",
      "\n",
      "Returns the \u001b[32mfloat\u001b[0m truncated to an Integer.\n",
      "\n",
      "Synonyms are #to_i, #to_int, and #truncate.\n"
     ]
    }
   ],
   "source": [
    "#show-doc Float#ceil\n",
    "show-doc Float#floor\n",
    "#show-doc Float#round\n",
    "#show-doc Float#truncate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 絶対値\n",
    "\n",
    "* abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0.6\n",
      "1.7\n"
     ]
    }
   ],
   "source": [
    "puts -2.abs\n",
    "puts -0.6.abs\n",
    "puts 1.7.abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fixnum\n",
      "Float\n"
     ]
    }
   ],
   "source": [
    "puts -2.class\n",
    "puts 1.7.class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m abs()\n",
      "\u001b[1mNumber of lines:\u001b[0m 4\n",
      "\n",
      "Returns the absolute value of \u001b[32mfix\u001b[0m.\n",
      "\n",
      "   \u001b[1;34m-12345\u001b[0m.abs   \u001b[1;34m#=> 12345\u001b[0m\n",
      "   \u001b[1;34m12345\u001b[0m.abs    \u001b[1;34m#=> 12345\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#abs\n",
    "#show-doc Float#abs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 数値を使った繰り返し\n",
    "\n",
    "* step\n",
    "* upto\n",
    "* downto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step\n",
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "upto\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "downto\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"step\"\n",
    "0.step(10, 2) do |i|\n",
    "  puts i\n",
    "end\n",
    "\n",
    "puts \"upto\"\n",
    "0.upto(3) do |i|\n",
    "  puts i\n",
    "end\n",
    "\n",
    "puts \"downto\"\n",
    "3.downto(0) do |i|\n",
    "  puts i\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Integer\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m downto(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 8\n",
      "\n",
      "Iterates the given block, passing decreasing values from \u001b[32mint\u001b[0m down to and\n",
      "including \u001b[32mlimit\u001b[0m.\n",
      "\n",
      "If no block is given, an Enumerator is returned instead.\n",
      "\n",
      "   \u001b[1;34m5\u001b[0m.downto(\u001b[1;34m1\u001b[0m) { |n| print n, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m.. \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m }\n",
      "   print \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m  Liftoff!\u001b[1;35m\\n\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "   \u001b[1;34m#=> \"5.. 4.. 3.. 2.. 1..   Liftoff!\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "#show-doc Fixnum#step\n",
    "#show-doc Fixnum#upto\n",
    "show-doc Fixnum#downto"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 小数を使った繰り返し"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5\n",
      "3.0\n",
      "4.5\n",
      "6.0\n",
      "7.5\n",
      "9.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.5"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.5.step(10,1.5) {|f| puts f}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Numeric\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m step(*arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 45\n",
      "\n",
      "Invokes the given block with the sequence of numbers starting at \u001b[32mnum\u001b[0m,\n",
      "incremented by \u001b[32mstep\u001b[0m (defaulted to \u001b[32m1\u001b[0m) on each call.\n",
      "\n",
      "The loop finishes when the value to be passed to the block is greater than\n",
      "\u001b[32mlimit\u001b[0m (if \u001b[32mstep\u001b[0m is positive) or less than \u001b[32mlimit\u001b[0m (if \u001b[32mstep\u001b[0m is\n",
      "negative), where \u001b[1mlimit\u001b[0m is defaulted to infinity.\n",
      "\n",
      "In the recommended keyword argument style, either or both of\n",
      "\u001b[32mstep\u001b[0m and \u001b[32mlimit\u001b[0m (default infinity) can be omitted.  In the\n",
      "fixed position argument style, zero as a step\n",
      "(i.e. num.step(limit, 0)) is not allowed for historical\n",
      "compatibility reasons.\n",
      "\n",
      "If all the arguments are integers, the loop operates using an integer\n",
      "counter.\n",
      "\n",
      "If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed the following expression:\n",
      "\n",
      "floor(n + n*epsilon)+ 1\n",
      "\n",
      "Where the \u001b[32mn\u001b[0m is the following:\n",
      "\n",
      "n = (limit - num)/step\n",
      "\n",
      "Otherwise, the loop starts at \u001b[32mnum\u001b[0m, uses either the less-than (<) or\n",
      "greater-than (>) operator to compare the counter against \u001b[32mlimit\u001b[0m, and\n",
      "increments itself using the + operator.\n",
      "\n",
      "If no block is given, an Enumerator is returned instead.\n",
      "\n",
      "For example:\n",
      "\n",
      "   p \u001b[1;34m1\u001b[0m.step.take(\u001b[1;34m4\u001b[0m)\n",
      "   p \u001b[1;34m10\u001b[0m.step(\u001b[35mby\u001b[0m: \u001b[1;34m-1\u001b[0m).take(\u001b[1;34m4\u001b[0m)\n",
      "   \u001b[1;34m3\u001b[0m.step(\u001b[35mto\u001b[0m: \u001b[1;34m5\u001b[0m) { |i| print i, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m }\n",
      "   \u001b[1;34m1\u001b[0m.step(\u001b[1;34m10\u001b[0m, \u001b[1;34m2\u001b[0m) { |i| print i, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m }\n",
      "   \u001b[1;34;4mMath\u001b[0m::\u001b[1;34;4mE\u001b[0m.step(\u001b[35mto\u001b[0m: \u001b[1;34;4mMath\u001b[0m::\u001b[1;34;4mPI\u001b[0m, \u001b[35mby\u001b[0m: \u001b[1;35m0.2\u001b[0m) { |f| print f, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m }\n",
      "\n",
      "Will produce:\n",
      "\n",
      "   [\u001b[1;34m1\u001b[0m, \u001b[1;34m2\u001b[0m, \u001b[1;34m3\u001b[0m, \u001b[1;34m4\u001b[0m]\n",
      "   [\u001b[1;34m10\u001b[0m, \u001b[1;34m9\u001b[0m, \u001b[1;34m8\u001b[0m, \u001b[1;34m7\u001b[0m]\n",
      "   \u001b[1;34m3\u001b[0m \u001b[1;34m4\u001b[0m \u001b[1;34m5\u001b[0m\n",
      "   \u001b[1;34m1\u001b[0m \u001b[1;34m3\u001b[0m \u001b[1;34m5\u001b[0m \u001b[1;34m7\u001b[0m \u001b[1;34m9\u001b[0m\n",
      "   \u001b[1;35m2.71828182845905\u001b[0m \u001b[1;35m2.91828182845905\u001b[0m \u001b[1;35m3.11828182845905\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "### 少数の繰り返しはFloatクラスではなくNumericクラスのメソッド\n",
    "show-doc Float#step"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Integerクラス"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 整数のべき乗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "puts 2.**4\n",
    "puts 2 ** 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 整数の除算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3.3333333333333335\n",
      "3.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "puts 10 / 3\n",
    "puts 10 / 3.0\n",
    "puts 10.0 / 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 整数に対する文字を求める\n",
    "\n",
    "* [アスキーコード](http://www9.plala.or.jp/sgwr-t/c_sub/ascii.html)に対応する文字を求める\n",
    "    * chrメソッド\n",
    "        * 範囲外の整数は<font color=\"red\">RangeError</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#\n",
      "0\n",
      "A\n"
     ]
    },
    {
     "ename": "Encoding::UndefinedConversionError",
     "evalue": "\"\\x80\" from ASCII-8BIT to UTF-8",
     "output_type": "error",
     "traceback": [
      "\u001b[31mEncoding::UndefinedConversionError\u001b[0m: \"\\x80\" from ASCII-8BIT to UTF-8",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib/multi_json/adapters/json_common.rb:19:in `encode'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib/multi_json/adapters/json_common.rb:19:in `to_json'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib/multi_json/adapters/json_common.rb:19:in `dump'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib/multi_json/adapter.rb:25:in `dump'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib/multi_json.rb:139:in `dump'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/session/mixin.rb:11:in `serialize'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/session/rbczmq.rb:56:in `send'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/ostream.rb:30:in `write'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/ostream.rb:38:in `block in puts'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/ostream.rb:38:in `each'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/ostream.rb:38:in `puts'\u001b[0m",
      "\u001b[37m(pry):4:in `puts'\u001b[0m",
      "\u001b[37m(pry):4:in `<main>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "puts 35.chr\n",
    "puts 48.chr\n",
    "puts 65.chr\n",
    "puts 128.chr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Integer\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m chr(*arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 6\n",
      "\n",
      "Returns a string containing the character represented by the \u001b[32mint\u001b[0m's value\n",
      "according to \u001b[32mencoding\u001b[0m.\n",
      "\n",
      "   \u001b[1;34m65\u001b[0m.chr    \u001b[1;34m#=> \"A\"\u001b[0m\n",
      "   \u001b[1;34m230\u001b[0m.chr   \u001b[1;34m#=> \"\\346\"\u001b[0m\n",
      "   \u001b[1;34m255\u001b[0m.chr(\u001b[1;34;4mEncoding\u001b[0m::\u001b[1;34;4mUTF_8\u001b[0m)   \u001b[1;34m#=> \"\\303\\277\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#chr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 次の整数、前の整数を求める\n",
    "\n",
    "* next\n",
    "    * 次の整数\n",
    "    * Integerクラスのインスタンスメソッド\n",
    "* succ\n",
    "    * 次の整数\n",
    "    * Fixnumクラスのインスタンスメソッド\n",
    "* pred\n",
    "    * 前の整数\n",
    "    * Integerクラスのインスタンスメソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n",
      "11\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "puts 10.next\n",
    "puts 10.succ\n",
    "puts 10.pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "succはFixnumはFixnumのインスタンスメソッドだが、BignumはIntegerのインスタンスメソッド\n",
      "#<UnboundMethod: Fixnum#succ>\n",
      "#<UnboundMethod: Bignum(Integer)#succ>\n",
      "nextとpredはFixnumクラスでは無くIntegerクラスのインスタンスメソッド\n",
      "#<UnboundMethod: Fixnum(Integer)#next>\n",
      "#<UnboundMethod: Fixnum(Integer)#pred>\n"
     ]
    }
   ],
   "source": [
    "puts \"succはFixnumはFixnumのインスタンスメソッドだが、BignumはIntegerのインスタンスメソッド\"\n",
    "puts Fixnum.instance_method(:succ)\n",
    "puts Bignum.instance_method(:succ)\n",
    "\n",
    "puts \"nextとpredはFixnumクラスでは無くIntegerクラスのインスタンスメソッド\"\n",
    "puts Fixnum.instance_method(:next)\n",
    "puts Fixnum.instance_method(:pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m succ()\n",
      "\u001b[1mNumber of lines:\u001b[0m 4\n",
      "\n",
      "Returns the Integer equal to \u001b[32mint\u001b[0m + 1.\n",
      "\n",
      "   \u001b[1;34m1\u001b[0m.next      \u001b[1;34m#=> 2\u001b[0m\n",
      "   (\u001b[1;34m-1\u001b[0m).next   \u001b[1;34m#=> 0\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "### ドキュメント無いものもある？\n",
    "#show-doc Integer#next\n",
    "#show-doc Integer#pred\n",
    "#show-doc Integer#succ\n",
    "show-doc Fixnum#succ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 整数を使った繰り返し\n",
    "\n",
    "* times\n",
    "    * 整数の回数繰り返す\n",
    "* upto\n",
    "    * 整数の数を増やしながら繰り返す\n",
    "* downto\n",
    "    * 整数の数を減らしながら繰り返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "times\n",
      "Hi\n",
      "Hi\n",
      "Hi\n",
      "upto\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "downto\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"times\"\n",
    "3.times{ puts \"Hi\"}\n",
    "puts \"upto\"\n",
    "0.upto(3){|i| puts i}\n",
    "puts \"downto\"\n",
    "3.downto(0){|i| puts i}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#<UnboundMethod: Fixnum(Integer)#times>\n",
      "#<UnboundMethod: Fixnum(Integer)#upto>\n",
      "#<UnboundMethod: Fixnum(Integer)#downto>\n"
     ]
    }
   ],
   "source": [
    "### Integerクラスのインスタンスメソッド\n",
    "puts Fixnum.instance_method(:times)\n",
    "puts Fixnum.instance_method(:upto)\n",
    "puts Fixnum.instance_method(:downto)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Integer\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m times()\n",
      "\u001b[1mNumber of lines:\u001b[0m 9\n",
      "\n",
      "Iterates the given block \u001b[32mint\u001b[0m times, passing in values from zero to\n",
      "int - \u001b[1;34m1\u001b[0m.\n",
      "\n",
      "If no block is given, an Enumerator is returned instead.\n",
      "\n",
      "   \u001b[1;34m5\u001b[0m.times \u001b[32mdo\u001b[0m |i|\n",
      "     print i, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "   \u001b[32mend\u001b[0m\n",
      "   \u001b[1;34m#=> 0 1 2 3 4\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#times\n",
    "#show-doc Fixnum#upto\n",
    "#show-doc Fixnum#downto"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Fixnumクラス、Bignumクラス(Ruby2.3以前)\n",
    "\n",
    "* Ruby2.4以降\n",
    "    * <font color=\"red\">Fixnum, Bignumは全てIntegerクラスにまとめられた</font>\n",
    "* Fixnum\n",
    "    * 通常の整数の数値リテラルはFixnumクラスのオブジェクトになる\n",
    "* Bignum\n",
    "    * 数値が大きい場合\n",
    "    * 自動的に拡張される\n",
    "* インスタンスメソッドはほぼ一緒"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 数値の除算\n",
    "\n",
    "* 除算\n",
    "    * 余りを求める\n",
    "* メソッド\n",
    "    * modulo\n",
    "    * %"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "puts 8.modulo(3)\n",
    "puts 8 % 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#<UnboundMethod: Fixnum#modulo>\n",
      "#<UnboundMethod: Fixnum#%>\n"
     ]
    }
   ],
   "source": [
    "puts Fixnum.instance_method(:modulo)\n",
    "puts Fixnum.instance_method(:%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m %(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 3\n",
      "\n",
      "Returns \u001b[32mfix\u001b[0m modulo \u001b[32mother\u001b[0m.\n",
      "\n",
      "See Numeric#divmod for more information.\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m modulo(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 3\n",
      "\n",
      "Returns \u001b[32mfix\u001b[0m modulo \u001b[32mother\u001b[0m.\n",
      "\n",
      "See Numeric#divmod for more information.\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#modulo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## ビット演算\n",
    "\n",
    "* メソッド\n",
    "    * |\n",
    "        * 論理和\n",
    "            * どちらか(もしくは両方)が1のとき1\n",
    "    * &\n",
    "        * 論理積\n",
    "            * 両方が1のとき1\n",
    "    * ^\n",
    "        * 排他的論理和\n",
    "            * 片方が1、もう片方が0のとき1\n",
    "    * ~\n",
    "        * 否定(ビット反転)\n",
    "    * `<<`\n",
    "        * 左シフト演算\n",
    "    * `>>`\n",
    "        * 右シフト演算\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10(0b1010)と3(0b0011)の論理和は11(0b1011)\n",
      "11\n",
      "10(0b1010)と3(0b0011)の論理積は2(0b0010)\n",
      "2\n",
      "10(0b1010)と3(0b0011)の排他的論理和は9(0b1001)\n",
      "9\n",
      "10(00..001010)の否定は-11(11..110101)\n",
      "-11\n",
      "10(0b1010)を一つ左にシフトすると20(0b10100)\n",
      "20\n",
      "10(0b1010)を一つ右にシフトすると5(0b0101)\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "puts \"10(0b1010)と3(0b0011)の論理和は11(0b1011)\"\n",
    "puts 10 | 3\n",
    "puts \"10(0b1010)と3(0b0011)の論理積は2(0b0010)\"\n",
    "puts 10 & 3\n",
    "puts \"10(0b1010)と3(0b0011)の排他的論理和は9(0b1001)\"\n",
    "puts 10 ^ 3\n",
    "puts \"10(00..001010)の否定は-11(11..110101)\"\n",
    "puts ~ 10\n",
    "puts \"10(0b1010)を一つ左にシフトすると20(0b10100)\"\n",
    "puts 10 << 1\n",
    "puts \"10(0b1010)を一つ右にシフトすると5(0b0101)\"\n",
    "puts 10 >> 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m >>(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 1\n",
      "\n",
      "Shifts \u001b[32mfix\u001b[0m right \u001b[32mcount\u001b[0m positions, or left if \u001b[32mcount\u001b[0m is negative.\n"
     ]
    }
   ],
   "source": [
    "#show-doc Fixnum#|\n",
    "#show-doc Fixnum#&\n",
    "#show-doc Fixnum#^\n",
    "#show-doc Fixnum#~\n",
    "show-doc Fixnum#<<\n",
    "#show-doc Fixnum#>>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 数値の小数化\n",
    "\n",
    "* 指数はeを使って表現できる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n",
      "\n",
      "指数表現(2 * 10^3)\n",
      "2000.0\n"
     ]
    }
   ],
   "source": [
    "puts 10.to_f\n",
    "puts\n",
    "\n",
    "puts \"指数表現(2 * 10^3)\"\n",
    "puts 2e+3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m numeric.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Fixnum\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m to_f()\n",
      "\u001b[1mNumber of lines:\u001b[0m 1\n",
      "\n",
      "Converts \u001b[32mfix\u001b[0m to a Float.\n"
     ]
    }
   ],
   "source": [
    "show-doc Fixnum#to_f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Complexクラス\n",
    "\n",
    "* Complexクラス\n",
    "    * Ruby1.9から組み込みクラス\n",
    "    * インスタンスメソッド\n",
    "        * real\n",
    "            * 実部を取得\n",
    "        * imaginary\n",
    "            * 虚部を取得"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2i\n",
      "2+3i\n",
      "3+4i\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "puts Complex(1,2)\n",
    "puts (2 + 3i)\n",
    "puts a = 3 + 4i\n",
    "puts a.real\n",
    "puts a.imaginary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m complex.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Complex\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m real()\n",
      "\u001b[1mNumber of lines:\u001b[0m 4\n",
      "\n",
      "Returns the real part.\n",
      "\n",
      "   Complex(\u001b[1;34m7\u001b[0m).real      \u001b[1;34m#=> 7\u001b[0m\n",
      "   Complex(\u001b[1;34m9\u001b[0m, \u001b[1;34m-4\u001b[0m).real  \u001b[1;34m#=> 9\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Complex#real"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m complex.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Complex\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m imaginary()\n",
      "\u001b[1mNumber of lines:\u001b[0m 4\n",
      "\n",
      "Returns the imaginary part.\n",
      "\n",
      "   Complex(\u001b[1;34m7\u001b[0m).imaginary      \u001b[1;34m#=> 0\u001b[0m\n",
      "   Complex(\u001b[1;34m9\u001b[0m, \u001b[1;34m-4\u001b[0m).imaginary  \u001b[1;34m#=> -4\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Complex#imaginary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 複素数から極座標への変換\n",
    "\n",
    "* abs\n",
    "    * 絶対値($r$)\n",
    "* arg\n",
    "    * 偏角($\\theta$)\n",
    "* polar\n",
    "    * 絶対値と偏角を配列で返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3,4,5の直角三角形の斜辺5\n",
      "5.0\n",
      "5,12,13の直角三角形の斜辺13\n",
      "13.0\n",
      "\n",
      "1 sqrt(3) 2の直角三角形。角度は30,60,90\n",
      "斜辺2\n",
      "1.9999999999999998\n",
      "偏角(ラジアン)\n",
      "1.0471975511965976\n",
      "角度60度\n",
      "59.99999999999999\n",
      "\n",
      "絶対値と偏角を配列で返す\n",
      "[1.9999999999999998, 1.0471975511965976]\n"
     ]
    }
   ],
   "source": [
    "puts \"3,4,5の直角三角形の斜辺5\"\n",
    "puts (3+4i).abs\n",
    "puts \"5,12,13の直角三角形の斜辺13\"\n",
    "puts (5+12i).abs\n",
    "puts \"\"\n",
    "\n",
    "puts \"1 sqrt(3) 2の直角三角形。角度は30,60,90\"\n",
    "c = Complex(1, Math.sqrt(3))\n",
    "puts \"斜辺2\"\n",
    "puts c.abs\n",
    "puts \"偏角(ラジアン)\"\n",
    "puts c.arg\n",
    "puts \"角度60度\"\n",
    "puts 180 * c.arg / Math::PI\n",
    "puts \"\"\n",
    "\n",
    "puts \"絶対値と偏角を配列で返す\"\n",
    "puts Complex(1, Math.sqrt(3)).polar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Complexクラスと他のNumericクラスとの四則演算\n",
    "\n",
    "* 四則演算の結果はComplexクラスのインスタンスで返る"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7+3i\n",
      "3.1+2i\n"
     ]
    }
   ],
   "source": [
    "puts 5 + (2+3i)\n",
    "puts 2.1 + (1+2i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Rationalクラス\n",
    "\n",
    "* Rationalクラス\n",
    "    * 有理数\n",
    "    * Ruby1.9から組み込みクラス\n",
    "* 使い所\n",
    "    * 整数と小数の比較\n",
    "        * 単純に少数で比較する場合は丸め誤差によるバグが発生する可能性がある\n",
    "        * Rationalクラスを使うと丸め誤差は気にしなくて良くなる\n",
    "* インスタンスメソッド\n",
    "    * numerator\n",
    "        * 分子を求める\n",
    "        * Floatクラスにも別実装で同じメソッドがある\n",
    "    * denominator\n",
    "        * 分母を求める\n",
    "        * Floatクラスにも別実装で同じメソッドがある\n",
    "    * divmod\n",
    "        * 引数で除算した結果をその余剰を配列で返す\n",
    "        * Rationalクラスでは無くNumericクラスのインスタンスメソッド\n",
    "        * Floatクラスにも別実装で同じメソッドがある"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/2\n",
      "1\n",
      "2\n",
      "0.25は有理数で1/4\n",
      "0.25\n",
      "1\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "puts Rational(1, 2)\n",
    "puts Rational(1, 2).numerator\n",
    "puts Rational(1, 2).denominator\n",
    "puts \"0.25は有理数で1/4\"\n",
    "puts 0.25\n",
    "puts 0.25.numerator\n",
    "puts 0.25.denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#<UnboundMethod: Float#numerator>\n",
      "#<UnboundMethod: Float#denominator>\n",
      "#<UnboundMethod: Float#divmod>\n",
      "#<UnboundMethod: Rational#numerator>\n",
      "#<UnboundMethod: Rational#denominator>\n",
      "#<UnboundMethod: Rational(Numeric)#divmod>\n"
     ]
    }
   ],
   "source": [
    "puts Float.instance_method(:numerator)\n",
    "puts Float.instance_method(:denominator)\n",
    "puts Float.instance_method(:divmod)\n",
    "puts Rational.instance_method(:numerator)\n",
    "puts Rational.instance_method(:denominator)\n",
    "puts Rational.instance_method(:divmod)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m rational.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Float\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m numerator()\n",
      "\u001b[1mNumber of lines:\u001b[0m 5\n",
      "\n",
      "Returns the numerator.  The result is machine dependent.\n",
      "\n",
      "   n = \u001b[1;35m0.3\u001b[0m.numerator    \u001b[1;34m#=> 5404319552844595\u001b[0m\n",
      "   d = \u001b[1;35m0.3\u001b[0m.denominator  \u001b[1;34m#=> 18014398509481984\u001b[0m\n",
      "   n.fdiv(d)            \u001b[1;34m#=> 0.3\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Float#numerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m rational.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Rational\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m numerator()\n",
      "\u001b[1mNumber of lines:\u001b[0m 6\n",
      "\n",
      "Returns the numerator.\n",
      "\n",
      "   Rational(\u001b[1;34m7\u001b[0m).numerator        \u001b[1;34m#=> 7\u001b[0m\n",
      "   Rational(\u001b[1;34m7\u001b[0m, \u001b[1;34m1\u001b[0m).numerator     \u001b[1;34m#=> 7\u001b[0m\n",
      "   Rational(\u001b[1;34m9\u001b[0m, \u001b[1;34m-4\u001b[0m).numerator    \u001b[1;34m#=> -9\u001b[0m\n",
      "   Rational(\u001b[1;34m-2\u001b[0m, \u001b[1;34m-10\u001b[0m).numerator  \u001b[1;34m#=> 1\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Rational#numerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 ÷ 3 = 3 余り 1\n",
      "[3, 1]\n",
      "\n",
      "9/4 ÷ 2 = 2.25 ÷ 2 = 1 余り 0.25 \n",
      "[1, (1/4)]\n",
      "[1, 0.25]\n"
     ]
    }
   ],
   "source": [
    "puts \"10 ÷ 3 = 3 余り 1\"\n",
    "puts 10.divmod(3)\n",
    "puts \"\"\n",
    "\n",
    "puts \"9/4 ÷ 2 = 2.25 ÷ 2 = 1 余り 0.25 \"\n",
    "puts Rational(9,4).divmod(2)\n",
    "puts Rational(9,4).divmod(2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Rationalクラスのインスタンスと異なるクラスのインスタンスの四則演算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9/4\n",
      "0.5\n",
      "0.5+3i\n"
     ]
    }
   ],
   "source": [
    "puts Rational(1, 4) + 2\n",
    "puts Rational(1, 4) + 0.25\n",
    "puts Rational(1, 4) + (0.25+3i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Rationalクラス変換"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "0.30000000000000004\n",
      "false\n",
      "\n",
      "3/10\n",
      "3/10\n",
      "true\n",
      "\n",
      "既に代入されている少数を有理数に変換\n",
      "3/10\n",
      "\n",
      "少数に戻す\n",
      "0.3\n"
     ]
    }
   ],
   "source": [
    "a = 3.0\n",
    "b = 0.1 * 3.0\n",
    "puts a\n",
    "puts b\n",
    "puts a == b\n",
    "puts\n",
    "\n",
    "c = 0.3r\n",
    "d = 0.1r * 3.0r\n",
    "puts c\n",
    "puts d\n",
    "puts c == d\n",
    "puts\n",
    "\n",
    "puts \"既に代入されている少数を有理数に変換\"\n",
    "e = 0.1\n",
    "f = 3.0\n",
    "\n",
    "g = e.rationalize * f.rationalize\n",
    "puts g\n",
    "puts \n",
    "\n",
    "puts \"少数に戻す\"\n",
    "puts g.to_f"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
